<?php

namespace App\Models;

class Conn
{
    public $conn;
    private $DB_HOST="rm-j6c58z7y12jm861gado.mysql.rds.aliyuncs.com";//外网
    //private $DB_HOST="jiujiu.rwlb.rds.aliyuncs.com";
    private $DB_USER="linglongshop";
    private $DB_PWD="3W5eWRjpTZl8aG";
    private $DB_NAME="wuyougou";
    //本地
    /*private $DB_HOST="127.0.0.1";//外网
    private $DB_USER="root";
    private $DB_PWD="aa123456";
    private $DB_NAME="linglong";*/

    public function __construct(){
        $this->conn = new \mysqli($this->DB_HOST,$this->DB_USER,$this->DB_PWD,$this->DB_NAME);
        if(!$this->conn){
            die("数据库链接失败".mysqli_error());
        }
        mysqli_query($this->conn,"set names 'utf8'");
    }
    /**
     * 普通查询
     * @return array
     * */
    public function readConn($sql,$all=false){
        $mysqli_result = mysqli_query($this->conn,$sql);
        if($all==false){
            $data = mysqli_fetch_assoc($mysqli_result);
        }else{
            while ($row = mysqli_fetch_assoc($mysqli_result)){
                $data[] = $row;
            }
        }

        //$conn -> close();
        return $data;
    }
    /**
     * 新增或者修改
     * @return int
     * */
    public function executeConn($sql){
        $mysqli_result = mysqli_query($this->conn,$sql);
        //$conn -> close();
        if($mysqli_result==true){
            return $this->conn->affected_rows;
        }else{
            return -1;
        }
    }
    /**
     * 返回上次插入的id
     * @return int
     * */
    public function mysql_insert_id ()
    {
        return mysqli_insert_id($this->conn);
    }
    /**
     * 开启事务
     * @return
     * */
    public function shiwuStart(){
        $this->conn-> autocommit(false);
    }
    /**
     * 回滚事务
     * @return
     * */
    public function shiwuRollback(){
        $this->conn-> rollback();
        $this->conn-> autocommit(true);
    }
    /**
     * 提交事务
     * @return
     * */
    public function shiwuEnd(){
        $this->conn-> commit();
        $this->conn-> autocommit(true);
    }

    public function close(){
        mysqli_close($this->conn);
    }
}